#include <iostream>
#include <poll.h>
#include <unistd.h>

#define NUM 128
struct pollfd rfd[NUM];

int main()
{
    rfd[0].fd = 0;
    rfd[0].events = POLLIN;
    rfd[0].revents = 0;
    while (true)
    {
        int n = poll(rfd, 1, -1);
        switch (n)
        {
        case 0:
            std::cout << "time out... " << std::endl;
            break;
        case -1:
            std::cout << "read error " << std::endl;
            break;
        default:
            //读取数据
            if(rfd[0].revents & POLLIN)
            {
                std::cout << "有数据到来了。。。" << std::endl;
                char buffer[1024] = {0};
                ssize_t s = read(0, buffer, sizeof(buffer)-1);
                if(s > 0)
                {
                    std::cout << "buffer: " << buffer << std::endl;
                }
                else if(s == 0)
                {
                    std::cout << "链接被关闭了。。。" << std::endl;
                    close(rfd[0].fd);
                }
                else
                {
                    std::cout << "读取失败。。。" << std::endl;
                    close(rfd[0].fd);
                }
            }
            break;
        }
        sleep(1);
    }
    return 0;
}